package com.lyl.excel.controller;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.lyl.excel.excel.Dept;
import com.lyl.excel.excel.DeptListener;
import com.lyl.excel.excel.Emp;
import com.lyl.excel.excel.EmpListener;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author Mr.Luo
 * @date 2022-09-30 14:53
 * @desc
 */
@RestController
public class ReadController {

    @PostMapping("/read")
    public void readMoreSheet(MultipartFile file) throws IOException {
        EasyExcelFactory.read(file.getInputStream(), Dept.class, new DeptListener()).sheet("dept").doRead();
        EasyExcelFactory.read(file.getInputStream(), Emp.class, new EmpListener()).sheet("emp").doRead();
    }

    @PostMapping("/read/v2")
    public void readMoreSheetV2(MultipartFile file) throws IOException {
        ExcelReader reader = EasyExcelFactory.read(file.getInputStream()).build();
        ReadSheet deptSheet = EasyExcelFactory.readSheet("dept").head(Dept.class).registerReadListener(new DeptListener()).build();
        ReadSheet empSheet = EasyExcelFactory.readSheet("emp").head(Emp.class).registerReadListener(new EmpListener()).build();
        reader.read(deptSheet, empSheet);
        reader.finish();
    }
}
